package marmalius;
/*
	Scape - The Scape Developers Team
	Phate, AridTag, Dan, DrBone420, Hero
	modifie par Metos [EOAH]
*/
//import java.io.*;
import java.sql.*;

import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class ItemHandler {

	// Phate: Setting VARS
	public static int showItemTimer = 60;
	public static int hideItemTimer = 60;

	// Phate: Global Item VARS
	public static int[] globalItemController = new int[5001];
	public static int[] globalItemID = new int[5001];
	public static int[] globalItemX = new int[5001];
	public static int[] globalItemY = new int[5001];
	public static int[] globalItemAmount = new int[5001];
	public static boolean[] globalItemStatic = new boolean[5001];

	public static int[] globalItemTicks = new int[5001];

	public ItemHandler() {			
		for (int i = 0; i <= 5000; i++) {
			globalItemController[i] = 0;
			globalItemID[i] = 0;
			globalItemX[i] = 0;
			globalItemY[i] = 0;
			globalItemAmount[i] = 0;
			globalItemTicks[i] = 0;
			globalItemStatic[i] = false;
		}
		//for (int i = 0; i < MaxDropItems; i++)
		//	ResetItem(i);
		for (int i = 0; i < MaxListedItems; i++) {
			ItemList[i] = null;
		}
		loadItemList();
	}

	public void process() {
		for (int i = 0; i <= 5000; i++) {
			if (globalItemID[i] != 0) {
				globalItemTicks[i]++;
			}
			if ((hideItemTimer + showItemTimer) == globalItemTicks[i]) {
				// misc.println("Item Removed ["+i+"] id is ["+globalItemID[i]+"]");
				if (!globalItemStatic[i]) {
					removeItemAll(globalItemID[i], globalItemX[i], globalItemY[i]);
				}
				else {
					//misc.println("Item is static");
				}
			}

			if (showItemTimer == globalItemTicks[i]) { // Phate: Item has expired, show to all
				if (!globalItemStatic[i]) {
					createItemAll(globalItemID[i], globalItemX[i], globalItemY[i], globalItemAmount[i], globalItemController[i]);
				}
				else {
					//misc.println("Item is static");
				}
			} 
			
		}
	}

	public static void removeGenItem(int itemID, int itemX, int itemY, int itemAmount) {
		for (int i = 0; i <= 5000; i++) {	
			if (globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY && globalItemAmount[i] == itemAmount) {
				globalItemController[i] = 0;
				globalItemID[i] = 0;
				globalItemX[i] = 0;
				globalItemY[i] = 0;
				globalItemAmount[i] = 0;
				globalItemTicks[i] = 0;
				globalItemStatic[i] = false;
				return;
			}
		}
	}

	public static boolean itemExists(int itemID, int itemX, int itemY) {
		for (int i = 0; i <= 5000; i++) { // Phate: Loop through all item spots
			if (globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY) { // Phate: Found item
				return true;
			}
		}
		return false; // Phate: Item doesnt exist
	}

	public static int itemAmount(int itemID, int itemX, int itemY) {
		for (int i = 0; i <= 5000; i++) { // Phate: Loop through all item spots
			if (globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY) { // Phate: Found item
				return globalItemAmount[i];
			}
		}
		return 0; // Phate: Item doesnt exist
	}

	public static void addItem(int itemID, int itemX, int itemY, int itemAmount, int itemController, boolean itemStatic) {
		for (int i = 0; i <= 5000; i++) { // Phate: Loop through all item spots
			if (globalItemID[i] == 0) { // Phate: Found blank item spot
				globalItemController[i] = itemController;
				globalItemID[i] = itemID;
				globalItemX[i] = itemX;
				globalItemY[i] = itemY;
				globalItemAmount[i] = itemAmount;
				globalItemStatic[i] = itemStatic;
				globalItemTicks[i] = 0;
				if (globalItemController[i] != -1 && globalItemController[i] != 0) {
					spawnItem(globalItemID[i], globalItemX[i], globalItemY[i], globalItemAmount[i], globalItemController[i]);
				}
				break;
			}
		}
	}

	public static void spawnItem(int itemID, int itemX, int itemY, int itemAmount, int playerFor) {
		client person = (client) PlayerHandler.getInstance().getPlayer(playerFor);
		if (person != null) {
			person.createGroundItem(itemID, itemX, itemY, itemAmount);
		}
	}

	public static void removeItem(int itemID, int itemX, int itemY, int itemAmount) {
		for (int i = 0; i <= 5000; i++) { // Phate: Loop through all item spots
			if (globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY && globalItemAmount[i] == itemAmount) {
				removeItemAll(globalItemID[i], globalItemX[i], globalItemY[i]);
				globalItemController[i] = 0;
				globalItemID[i] = 0;
				globalItemX[i] = 0;
				globalItemY[i] = 0;
				globalItemAmount[i] = 0;
				globalItemTicks[i] = 0;
				globalItemStatic[i] = false;
				break;
			}
		}
	}

	public static void createItemAll(int itemID, int itemX, int itemY, int itemAmount, int itemController) {
		for (Player p : PlayerHandler.getInstance().getAllPlayer()) {
			if (p != null) {
				client person = (client) p;
				if ((person.playerName != null || person.playerName != "null") && !(person.playerId == itemController)) {
					// misc.println("distance to create "+person.distanceToPoint(itemX, itemY));
					if (person.distanceToPoint(itemX, itemY) <= 60) {
						person.createGroundItem(itemID, itemX, itemY, itemAmount);
					}
				}
			}
		}
	}

	public static void removeItemAll(int itemID, int itemX, int itemY) {
		for (Player p : PlayerHandler.getInstance().getAllPlayer()) {
			if (p != null) {
				client person = (client) p;
				if (person.playerName != null || person.playerName != "null") {
					// misc.println("distance to remove "+person.distanceToPoint(itemX, itemY));
					if (person.distanceToPoint(itemX, itemY) <= 60)
						person.removeGroundItem(itemID, itemX, itemY);
				}
			}
		}
	}
	
	//public static int DropItemCount = 0;
	//public static int MaxDropItems = 100000;
	public static int MaxListedItems = 10000;
	// process() is called evry 500 ms
	//public static int MaxDropShowDelay = 120; // 120 * 500 = 60000 / 1000 = 60s
	public static int SDID = 90; // 90 * 500 = 45000 / 1000 = 45s
	// SDID = Standard Drop Items Delay
	//public static int[] DroppedItemsID = new int[MaxDropItems];
	//public static int[] DroppedItemsX = new int[MaxDropItems];
	//public static int[] DroppedItemsY = new int[MaxDropItems];
	//public static int[] DroppedItemsN = new int[MaxDropItems];
	//public static int[] DroppedItemsH = new int[MaxDropItems];
	//public static int[] DroppedItemsDDelay = new int[MaxDropItems];
	//public static int[] DroppedItemsSDelay = new int[MaxDropItems];
	//public static int[] DroppedItemsDropper = new int[MaxDropItems];
	//public static int[] DroppedItemsDeletecount = new int[MaxDropItems];
	//public static boolean[] DroppedItemsAlwaysDrop = new boolean[MaxDropItems];
	public ItemList ItemList[] = new ItemList[MaxListedItems];

	/* ItemHandler() {
	 for(int i = 0; i < MaxDropItems; i++) {
	 ResetItem(i);
	 }
	 for(int i = 0; i < MaxListedItems; i++) {
	 ItemList[i] = null;
	 }
	 loadItemList("item.cfg");
	 }*/
	
	public void newItemList(int ItemId, String ItemName, String ItemDescription, double ShopValue, double LowAlch, double HighAlch, int Bonuses[]) {
		// first, search for a free slot
		int slot = -1;

		for (int i = 0; i < 9999; i++) {
			if (ItemList[i] == null) {
				slot = i;
				break;
			}
		}

		if (slot == -1)// no free slot found
			return;	

		ItemList newItemList = new ItemList(ItemId);

		newItemList.itemName = ItemName;
		newItemList.itemDescription = ItemDescription;
		newItemList.ShopValue = ShopValue;
		newItemList.LowAlch = LowAlch;
		newItemList.HighAlch = HighAlch;
		newItemList.Bonuses = Bonuses;
		ItemList[slot] = newItemList;
	}

	
	public boolean loadItemList() {
		try {
			int nb = 0;
			
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM item_template");
			while (rs.next()) {
				int[] Bonuses = new int[12];
				
				for (int i = 0; i <= 11; i++)
					Bonuses[i] = rs.getInt("b"+i);
				
				newItemList(rs.getInt("id"), rs.getString("name"), rs.getString("description"), rs.getDouble("shopvalue"), rs.getDouble("lowalch"), rs.getDouble("highalch"), Bonuses);
				nb++;
			}
			LogService.getInstance().logInfo("item_template load ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load item_template.");
			LogService.getInstance().logError(ex.getMessage());
		}
		return false;
	}

    public boolean isExistItemId (int id) {
        if (id > 0 || id < 9999) {
            if (ItemList[id] != null)
                return true;
        }
        return false;

    }
}